const date = new Date();
const MD = {
  1: 31,
  2: 28,
  3: 31,
  4: 30,
  5: 31,
  6: 30,
  7: 31,
  8: 31,
  9: 30,
  10: 31,
  11: 30,
  12: 31
}
var dayLength = 0
function onloadSet(multiArray, e, month = date.getMonth() + 1, day = date.getDate()) {
  let years = []
  let months = []
  let days = []
  let hours = []
  if (month == 12 && day > 25) {
    for (let i = date.getFullYear(); i <= date.getFullYear() + 1; i++) {
      years.push("" + i + '年');
    }
  } else {
    years.push("" + date.getFullYear() + '年');
  }
  if (parseInt(e) == date.getFullYear()) {
    let month_length = month
    if ((day + 7) > MD[month] && month < 12) {
      month_length = month + 1
      dayLength = (day + 7) - MD[month]
    }
    //获取月份
    for (let i = month; i <= month_length; i++) {
      if (i < 10) {
        i = "0" + i;
      }
      months.push("" + i + '月');
    }
    //获取日期
    for (let i = day; i <= MD[month]; i++) {
      if (i < 10) {
        i = "0" + i;
      }
      days.push("" + i + '日');
    }
    //获取小时
    for (let i = date.getHours() + 1; i < 24; i++) {
      if (i < 10) {
        i = "0" + i;
      }
      hours.push("" + i + '时');
    }
    return [years, months, days, hours, ["00分", '30分']];
  } else {
    // 选择非当前年份
    //获取月份
    for (let i = 1; i <= 1; i++) {
      if (i < 10) {
        i = "0" + i;
      }
      months.push("" + i + '月');
    }
    //获取日期
    for (let i = 1; i <= ((day + 7) - MD[month]); i++) {
      if (i < 10) {
        i = "0" + i;
      }
      days.push("" + i + '日');
    }
    //获取小时
    for (let i = 0; i < 24; i++) {
      if (i < 10) {
        i = "0" + i;
      }
      hours.push("" + i + '时');
    }
    return [multiArray[0], months, days, hours, ["00分", '30分']];
  }
}
//获取时间日期
function bindMultiPickerChange(e, multiArray) {
  let multiIndex = e.detail.value;
  const index = multiIndex;
  const year = parseInt(multiArray[0][index[0]]);
  const month = parseInt(multiArray[1][index[1]]);
  const day = parseInt(multiArray[2][index[2]]);
  const hour = parseInt(multiArray[3][index[3]]);
  const minute = parseInt(multiArray[4][index[4]]);
  return [year, month >= 10 ? month : '0' + month, day >= 10 ? day : '0' + day, hour >= 10 ? hour : '0' + hour, minute > 0 ? minute : '00']
}
//监听picker的滚动事件
function bindMultiPickerColumnChange(e, multiArray, multiIndex, choose_year) {
  multiIndex[e.detail.column] = e.detail.value;
  let num = parseInt(multiArray[e.detail.column][e.detail.value]);
  let temp = [];
  let temp_hour = [];
  let isYear = (parseInt(choose_year) === date.getFullYear()) ? true : false;
  let isMonth = (num === (date.getMonth() + 1)) ? true : false;
  let isDay = false;
  // 滚动月份天数改变
  if (e.detail.column == 1) {
    // 当前月
    if (isMonth) {
      for (let i = 1; i <= MD[num]; i++) {
        if (i < 10) {
          i = "0" + i;
        }
        temp.push("" + i + '日');
      }
      // 当前年当前月去除已过去的日期
      if (isYear) {
        temp = temp.slice(date.getDate() - 1)
      }
    } else {
      for (let i = 1; i <= (dayLength - 1); i++) {
        if (i < 10) {
          i = "0" + i;
        }
        temp.push("" + i + '日');
      }
    }
    multiArray[2] = temp;
  }
  // 滚动年月日 时间变动 
  if (e.detail.column == 0 || e.detail.column == 1 || e.detail.column == 2) {
    isMonth = (parseInt(multiArray[1][multiIndex[1]]) === (date.getMonth() + 1)) ? true : false;
    isDay = (parseInt(multiArray[2][multiIndex[2]]) === date.getDate()) ? true : false;
    // 当前年月日
    if (isYear && isMonth && isDay) {
      for (let i = date.getHours() + 1; i < 24; i++) {
        if (i < 10) {
          i = "0" + i;
        }
        temp_hour.push("" + i + '时');
      }
    } else {
      for (let i = 0; i < 24; i++) {
        if (i < 10) {
          i = "0" + i;
        }
        temp_hour.push("" + i + '时');
      }
    }
    multiArray[3] = temp_hour;
  }
  var data = {
    multiArray: multiArray,
    multiIndex: multiIndex,
  };
  return data;
}
module.exports = {
  onloadSet,
  bindMultiPickerChange,
  bindMultiPickerColumnChange
};